package exp5;

// import com.sun.corba.se.impl.orbutil.ObjectUtility;

//---------------------------------------------------------------
//  SocketClient_3.java   Author: 唐才铭   Date：2018-06-13
//
//  基于Java Socket实现客户端功能，传输方式用TCP
//  客户端让用户输入中缀表达式，然后把中缀表达式调用MyBC.java的
//  功能转化为后缀表达式，把后缀表达式用3DES或AES算法加密后通过
//  网络把密文发送给服务器通过网络发送给服务器；
//  显示服务器发送过来的结果。
//---------------------------------------------------------------
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created by tcm on 2018/6/13.
 */
public class SocketClient_3 {
    public static void main(String[] args) throws IOException {
        //1.建立客户端Socket连接，指定服务器位置和端口
        // Socket socket = new Socket("localhost",8080);
        Socket socket = new Socket("192.168.1.155",8800);

        //2.得到socket读写流
        OutputStream outputStream = socket.getOutputStream();
        // PrintWriter printWriter = new PrintWriter(outputStream);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        //输入流
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        //3.利用流按照一定的操作，对socket进行读写操作

        //客户端让用户输入中缀表达式，然后把中缀表达式调用MyBC.java的功能转化为后缀表达式
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入中缀表达式");
        String info1 = scanner.nextLine();
        info1 = MyBC.infixToSuffix(info1);

        //把后缀表达式用3DES或AES算法加密后通过网络把密文发送给服务器
        try {
            info1 = SEnc.encrypt(info1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String info = new String(info1.getBytes("GBK"),"utf-8");
        //     printWriter.write(info);
        //     printWriter.flush();
        outputStreamWriter.write(info);
        outputStreamWriter.flush();
        socket.shutdownOutput();
        //接收服务器的响应
        String reply = null;
        while (!((reply = bufferedReader.readLine()) ==null)){
            System.out.println("\n" + "接收服务器的信息为：" + reply);
        }
        //4.关闭资源
        bufferedReader.close();
        inputStream.close();
        outputStreamWriter.close();
        //printWriter.close();
        outputStream.close();
        socket.close();
    }
}